<?php
/**
 * Created by zengwei.
 * Date: 2019/10/12
 * Time: 14:09
 * Function:
 */

namespace App\Utilities;


use GuzzleHttp\Client;

class GaodeMaps
{
    public static function geocodeAddress($state,$city,$address){
        //省市地址
        $address = urlencode($state.$city.$address);
        $apiKey = config('services.gaode.ws_api_key');
        $url = "https://restapi.amap.com/v3/geocode/geo?key={$apiKey}&address={$address}";

        $client = new Client();
        $response = $client->get($url)->getBody();
        $geoData = json_decode($response);
        $position = [
            'lat'=>null,
            'lng'=>null,
        ];
        if($geoData&&$geoData->status&&$geoData->geocodes&&isset($geoData->geocodes[0])){
            list($latitude, $longitude) = explode(',',$geoData->geocodes[0]->location);
            $position['lat'] = $latitude;
            $position['lng'] = $longitude;
        }
        return $position;
    }

}
